﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Framework.Message.SMS.SGIP.Commands
{
    /// <summary>
    /// Bind操作由Bind命令和Bind_Resp应答组成。客户端首先发送Bind命令，服务器端收到Bind命令后，对命令发送方进行验证，然后返回Bind_Resp应答。
    /// </summary>
    public class BindCommand : Command
    {
        #region 变量与属性
        private BindLoginType _LoginType = BindLoginType.SpToSmg;
        /// <summary>
        /// 登录类型。
        /// </summary>
        public BindLoginType LoginType
        {
            get { return _LoginType; }
            set { _LoginType = value; }
        }

        private string _LoginName = "";
        /// <summary>
        /// 服务器端给客户端分配的登录名
        /// </summary>
        public string LoginName
        {
            get { return _LoginName; }
            set { _LoginName = value; }
        }

        private string _LoginPassowrd = "";
        /// <summary>
        /// 服务器端和Login Name对应的密码
        /// </summary>
        public string LoginPassowrd
        {
            get { return _LoginPassowrd; }
            set { _LoginPassowrd = value; }
        }

        private string _Reserve = "";

        public string Reserve
        {
            get { return _Reserve; }
            set { _Reserve = value; }
        }
        #endregion

        #region 构造函数
        public BindCommand()
        {
        }

        public BindCommand(byte[] bytes)
            : base(bytes)
        {
        }

        public BindCommand(byte[] bytes, int startIndex)
            : base(bytes, startIndex)
        {
        }

        public BindCommand(CommandSequence sequence)
            : base(sequence)
        {
        }
        #endregion

        public override uint BodyLength
        {
            get { return 1 + 16 + 16 + 8; }
        }

        public override SGIPCommandType CommandType
        {
            get { return SGIPCommandType.Bind; }
        }

        protected override void BytesTo(byte[] bytes, int startIndex)
        {
            int i = (int)CommandHeader.HeaderLength;
            base.BytesTo(bytes, startIndex);
            _LoginType = (BindLoginType)bytes[i];
            i++;
            _LoginName = ByteStringConvert.Bytes2String(bytes, i, 16, MessageCoding.ASCII).TrimEnd('\0');
            i += 16;
            _LoginPassowrd = ByteStringConvert.Bytes2String(bytes, i, 16, MessageCoding.ASCII).TrimEnd('\0');
            i += 16;
            Reserve = ByteStringConvert.Bytes2String(bytes, i, 8, MessageCoding.ASCII).TrimEnd('\0');
        }

        public override byte[] ToBytes()
        {
            int i = (int)CommandHeader.HeaderLength;
            byte[] bytes = base.ToBytes();
            bytes[i] = (byte)LoginType;
            i++;
            ByteStringConvert.String2Bytes(_LoginName.PadRight(16, '\0'), MessageCoding.ASCII).CopyTo(bytes, i);
            i += 16;
            ByteStringConvert.String2Bytes(_LoginPassowrd.PadRight(16, '\0'), MessageCoding.ASCII).CopyTo(bytes, i);
            i += 16;
            ByteStringConvert.String2Bytes(_Reserve.PadRight(8, '\0'), MessageCoding.ASCII).CopyTo(bytes, i);
            return bytes;
        }
    }
}
